home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_perl.idb / usr / freeware / lib / perl5 / 5.00502 / User / grent.pm.z / grent.pm
Encoding:
Perl POD Document  |  1998-10-28  |  2.8 KB  |  94 lines

  1. package User::grent;
  2. use strict;
  3.  
  4. BEGIN { 
  5.     use Exporter   ();
  6.     use vars       qw(@EXPORT @EXPORT_OK %EXPORT_TAGS);
  7.     @EXPORT      = qw(getgrent getgrgid getgrnam getgr);
  8.     @EXPORT_OK   = qw($gr_name $gr_gid $gr_passwd $gr_mem @gr_members);
  9.     %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
  10. }
  11. use vars      @EXPORT_OK;
  12.  
  13. # Class::Struct forbids use of @ISA
  14. sub import { goto &Exporter::import }
  15.  
  16. use Class::Struct qw(struct);
  17. struct 'User::grent' => [
  18.     name    => '$',
  19.     passwd  => '$',
  20.     gid        => '$',
  21.     members => '@',
  22. ];
  23.  
  24. sub populate (@) {
  25.     return unless @_;
  26.     my $gob = new();
  27.     ($gr_name, $gr_passwd, $gr_gid) = @$gob[0,1,2] = @_[0,1,2];
  28.     @gr_members = @{$gob->[3]} = split ' ', $_[3];
  29.     return $gob;
  30.  
  31. sub getgrent ( ) { populate(CORE::getgrent()) } 
  32. sub getgrnam ($) { populate(CORE::getgrnam(shift)) } 
  33. sub getgrgid ($) { populate(CORE::getgrgid(shift)) } 
  34. sub getgr    ($) { ($_[0] =~ /^\d+/) ? &getgrgid : &getgrnam } 
  35.  
  36. 1;
  37. __END__
  38.  
  39. =head1 NAME
  40.  
  41. User::grent - by-name interface to Perl's built-in getgr*() functions
  42.  
  43. =head1 SYNOPSIS
  44.  
  45.  use User::grent;
  46.  $gr = getgrgid(0) or die "No group zero";
  47.  if ( $gr->name eq 'wheel' && @{$gr->members} > 1 ) {
  48.      print "gid zero name wheel, with other members";
  49.  } 
  50.  
  51.  use User::grent qw(:FIELDS;
  52.  getgrgid(0) or die "No group zero";
  53.  if ( $gr_name eq 'wheel' && @gr_members > 1 ) {
  54.      print "gid zero name wheel, with other members";
  55.  } 
  56.  
  57.  $gr = getgr($whoever);
  58.  
  59. =head1 DESCRIPTION
  60.  
  61. This module's default exports override the core getgrent(), getgruid(),
  62. and getgrnam() functions, replacing them with versions that return
  63. "User::grent" objects.  This object has methods that return the similarly
  64. named structure field name from the C's passwd structure from F<grp.h>; 
  65. namely name, passwd, gid, and members (not mem).  The first three
  66. return scalars, the last an array reference.
  67.  
  68. You may also import all the structure fields directly into your namespace
  69. as regular variables using the :FIELDS import tag.  (Note that this still
  70. overrides your core functions.)  Access these fields as variables named
  71. with a preceding C<gr_>.  Thus, C<$group_obj-E<gt>gid()> corresponds
  72. to $gr_gid if you import the fields.  Array references are available as
  73. regular array variables, so C<@{ $group_obj-E<gt>members() }> would be
  74. simply @gr_members.
  75.  
  76. The getpw() funtion is a simple front-end that forwards
  77. a numeric argument to getpwuid() and the rest to getpwnam().
  78.  
  79. To access this functionality without the core overrides,
  80. pass the C<use> an empty import list, and then access
  81. function functions with their full qualified names.
  82. On the other hand, the built-ins are still available
  83. via the C<CORE::> pseudo-package.
  84.  
  85. =head1 NOTE
  86.  
  87. While this class is currently implemented using the Class::Struct
  88. module to build a struct-like class, you shouldn't rely upon this.
  89.  
  90. =head1 AUTHOR
  91.  
  92. Tom Christiansen
  93.